package io.zhifei.modules.sys.system.controller;

import cn.gjing.tools.excel.ExcelFactory;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import io.zhifei.common.utils.DateUtils;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.modules.sys.system.form.*;
import io.zhifei.modules.sys.system.form.member.MemberParam;
import io.zhifei.modules.sys.system.form.member.MemberResult;
import io.zhifei.modules.sys.system.service.MemberClassService;
import io.zhifei.modules.sys.system.service.member.MemberService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>功能描述:</p>
 *
 * @author : duke
 * @version 1.0.0<br>
 * @CreateTime 2020/10/15
 */
@RestController
@RequestMapping("/sys/memberClass")
@Api(value = "班级Controller", tags = "后台班级设置管理")
public class SysMemberClassController extends AbstractController {

    @Resource
    private MemberClassService memberClassService;

    @Resource
    private MemberService memberService;

    /**
     * 新增班级信息
     *
     * @param param 班级信息
     * @return
     */
    @PutMapping("/add")
    @ApiOperation(value = "添加班级信息", httpMethod = "PUT")
    public R add(MemberClassParam param) {
        param.setCreateUser(getUserId());
        param.setCreateTime(new Date());
        this.memberClassService.addMemberClass(param);
        return R.ok();
    }

    /**
     * 修改班级信息
     * @param param 班级信息
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改班级信息",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "班级ID", name = "classId", required = true)
    })
    public R update(@RequestBody MemberClassParam param){
        //当状态为禁用时
        if (null != param.getStatus() && 1 == param.getStatus()){
            MemberParam memberParam = MemberParam.builder()
                    .classId(param.getClassId())
                    .build();
            List<MemberResult> memberResults = this.memberService.getMemberList(memberParam);
            if (memberResults.size() != 0){
                return R.error("当前班级下有学员，不能禁用!");
            }
        }
        param.setCreateUser(getUserId());
        param.setCreateTime(new Date());
        this.memberClassService.updateMemberClass(param);
        return R.ok();
    }

    /**
     * 删除班级信息
     * @param param 班级信息
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除班级信息",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "班级ID", name = "classId", required = true)
    })
    public R delete(@ApiIgnore MemberClassParam param){
        param.setFlag(1);
        this.memberClassService.updateMemberClass(param);
        return R.ok();
    }

    /**
     * 获取班级分页列表
     * @param page 分页参数
     * @param param 查询条件
     * @return
     */
    @GetMapping("/getPageList")
    @ApiOperation(value = "获取班级分页列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", name = "current", required = true),
            @ApiImplicitParam(value = "页面大小", name = "size", required = true),
            @ApiImplicitParam(value = "班级名称和备考年份搜索字段",name = "nameAndPreparYear")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = MemberClassResult.class)
    })
    public R getMemberClassPageList(@ApiIgnore Page page, @ApiIgnore MemberClassParam param){
        PageUtils memberClassPageList = this.memberClassService.getMemberClassPageList(page,param);
        return R.ok().put("data",memberClassPageList);
    }

    /**
     * 获取班级分页列表
     * @param page 分页参数
     * @param param 查询条件
     * @return
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取班级列表",httpMethod = "GET")
    public R getMemberClassList(@ApiIgnore MemberClassParam param){
        if (StrUtil.isNotEmpty(param.getSelectClassIds())){
            param.setSelectClassIdsArray(param.getSelectClassIds().split(","));
        }
        List<MemberClassResult> memberClassList = this.memberClassService.getMemberClassList(param);
        return R.ok().put("data",memberClassList);
    }

    /**
     * 获取班级名称列表，树形输出
     */
    @GetMapping("/getClassList")
    @ApiOperation(value = "获取班级名称列表，树形输出",httpMethod = "GET")
    public R getMemberClass2List(){
        List<MemberClassTreeVo> memberClassList = this.memberClassService.getMemberClass2List();
        return R.ok().put("data",memberClassList);
    }



    /**
     * 获取班级详情
     * @param param 查询条件-班级ID
     * @return
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取班级详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "班级ID",name = "classId",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = MemberClassResult.class)
    })
    public R getMemberClassDetail(@ApiIgnore MemberClassParam param){
        MemberClassResult result = this.memberClassService.getMemberClassDetail(param);
        return R.ok().put("data",result);
    }

    /**
     * 课程进度统计
     * @param page 当前页
     * @param pageSize 页面大小
     * @param param 查询参数
     * @return
     */
    @GetMapping("/getCourseProgressStatistic")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "当前页大小", required = true),
            @ApiImplicitParam(name = "classId",value = "班级ID"),
            @ApiImplicitParam(name = "subject",value = "科目ID"),
            @ApiImplicitParam(name = "courseType",value = "课程类型，1：笔试、2：面试"),
            @ApiImplicitParam(name = "courseName",value = "课程名称"),
            @ApiImplicitParam(name = "userName",value = "用户真实姓名"),
    })
    @ApiOperation(value = "进度统计",httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = CourseProgressResult.class)
    })
    public R getCourseProgressStatistic(@ApiIgnore Integer page, @ApiIgnore Integer pageSize, @ApiIgnore CourseProgressResult param){
        PageUtils pageList = this.memberClassService.getCourseProgressStatistic(new Page(page,pageSize),param);
        return R.ok().put("data",pageList);
    }

    /**
     * 导出进度统计信息
     * @param response
     * @param customerParam 查询参数
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId",value = "班级ID"),
            @ApiImplicitParam(name = "subject",value = "科目ID"),
            @ApiImplicitParam(name = "courseType",value = "课程类型，1：笔试、2：面试"),
            @ApiImplicitParam(name = "courseName",value = "课程名称"),
            @ApiImplicitParam(name = "userName",value = "用户真实姓名"),
    })
    @ApiOperation(value = "导出进度统计信息",httpMethod = "GET")
    @GetMapping("/exportCourseProgressStatistic")
    public void exportCourseProgressStatistic(@ApiIgnore HttpServletResponse response, @ApiIgnore CourseProgressResult param) {
        //获取导出excel的列表
        List<CourseProgressResult> courseProgressResults = this.memberClassService.getCourseProgressStatisticList(param);
        for (CourseProgressResult courseProgressResult: courseProgressResults) {
            if (null != courseProgressResult.getProgressLengthResult()){
                //课程进度
                String courseLengthAndLearnLength =
                        DateUtils.formatSecondDateTime(courseProgressResult.getProgressLengthResult().getLearnLength())
                        + "/" + DateUtils.formatSecondDateTime(courseProgressResult.getProgressLengthResult().getCourseLength());
                //完成度
                String progressRatio = "0%";
                try {
                    progressRatio = Double.valueOf(courseProgressResult.getProgressLengthResult().getProgressRatio()) * 100 + "%";
                }catch (Exception e){
                }
                courseProgressResult.setCourseLengthAndLearnLength(courseLengthAndLearnLength);
                courseProgressResult.setProgressRatio(progressRatio);
            }
        }
        ExcelFactory.createWriter("学员进度统计信息"+System.currentTimeMillis(), CourseProgressResult.class, response)
                .write(courseProgressResults)
                .flush();
    }
}
